package com.xteam.db.page;

import java.io.Serializable;

/**
 * 分页信息对象 当从PAGE 传来一个PageInfo 对象时，只需要设置 每页的记录数量(pageSize) 目标页码(curtPage)， 就可以实现自动的分页功能 后台数据系统需要设置 总记录数,当前页记录数,总页数由本PAGE计算得出 Created by IntelliJ IDEA. User: tom
 * Date: 2006-10-21 Time: 12:21:47
 */
public class PageInfo implements Page, Serializable {
	
	private static final long	serialVersionUID	= -7198616930181053822L;

	/**
	 * 当前页的起始记录号
	 */
	public int		index		= 1;
	
	/**
	 * 每页记录数
	 */
	private int		size		= Page.PAGE_SIEZ;
	
	/**
	 * 页数
	 */
	private int		pageCount	= 1;
	
	/**
	 * 总记录数
	 */
	public long		rowCount	= 1;
	
	/**
	 * 当前页码
	 */
	private int		pageNo		= 1;

	/**
	 * 获取总记录数的SQL语句
	 */
	public String	RowCountSQL	= null;

	/**
	 * 获取总记录数的HQL语句
	 */
	public String	RowCountHQL	= null;

	/**
	 * 创建一个分页对象
	 * @author tangkf ---2012-1-15
	 * @param size 每页记录数
	 * @param pageNo 目标页码
	 * @return
	 */
	public static PageInfo create(int size, int pageNo) {
		return new PageInfo(size, pageNo);
	}

	/**
	 * 构造一个分页对象
	 * @author tangkf ---2012-1-15
	 * @param size 每页记录数
	 * @param pageNo 目标页码
	 */
	public PageInfo(int size, int pageNo) {
		this.size = size;
		this.pageNo = pageNo;
	}

	/**
	 * <P>
	 * 返回当前记录总数
	 * </P>
	 * @author 汤垲峰 2009-4-27
	 * @return
	 */
	public long getRowCount() {
		return rowCount;
	}

	/**
	 * @param curtRowCount 属性curtRowCount.
	 */
	public void setCurtRowCount(int curtRowCount) {
		this.index = curtRowCount;
	}

	/**
	 * @param rowCount 属性rowCount.
	 */
	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
		if (this.getPageNo() > this.getPageCount()) {
			this.setPageNo(1); // 到第一页
		} else if (this.getPageNo() < 1) {
			this.setPageNo(this.getPageCount()); // 到最后页
		}
	}

	/**
	 * 获取总页数
	 * @see com.xteam.db.page.toms.commons.dbserver.page.Page#getPageCount()
	 */
	public int getPageCount() {
		int c = 0;
		if (this.rowCount % size != 0)
			c = 1;
		return (int) (this.rowCount / size) + c;
	}

	/**
	 * 获取每页记录数
	 * @see com.xteam.db.page.toms.commons.dbserver.page.Page#getSize()
	 */
	public int getSize() {
		return size;
	}

	/**
	 * 设置每页记录数
	 * @see com.xteam.db.page.toms.commons.dbserver.page.Page#setSize(int)
	 */
	public void setSize(int pageSize) {
		if (pageSize < 1)
			pageSize = Page.PAGE_SIEZ;
		this.size = pageSize;
	}

	/**
	 * 获取当前页码
	 * @see com.xteam.db.page.toms.commons.dbserver.page.Page#getPageNo()
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * <P>
	 * 设置当前所在页码
	 * </P>
	 * @author 汤垲峰 2009-4-27
	 * @param curtPage
	 */
	public void setPageNo(int pageNo) {
		if (pageNo < 1)
			pageNo = pageCount;
		this.pageNo = pageNo;
	}

	/**
	 * 返回当前页起始记录号
	 * @see com.xteam.db.page.toms.commons.dbserver.page.Page#getIndex()
	 */
	public int getIndex() {
		return this.size * (this.pageNo - 1);
	}
	
	/**
	 * 返回 获取总记录数的SQL语句 一般建议为：select count(<ID>) from
	 * <TABLE>
	 * @return 属性rowCountSQL的值.
	 */
	public String getRowCountSQL() {
		return this.RowCountSQL;
	}

	/**
	 * 设置 获取总记录数的SQL语句 一般建议为：select count(<ID>) from
	 * <TABLE>
	 * @param rowCountSQL 属性rowCountSQL.
	 */
	public void setRowCountSQL(String rowCountSQL) {
		RowCountSQL = rowCountSQL;
	}

	/**
	 * @return 属性rowCountHQL的值.
	 */
	public String getRowCountHQL() {
		return this.RowCountHQL;
	}

	/**
	 * @param rowCountHQL 属性rowCountHQL.
	 */
	public void setRowCountHQL(String rowCountHQL) {
		RowCountHQL = rowCountHQL;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("PageInfo [RowCountHQL=");
		builder.append(RowCountHQL);
		builder.append(", RowCountSQL=");
		builder.append(RowCountSQL);
		builder.append(", index=");
		builder.append(index);
		builder.append(", pageCount=");
		builder.append(pageCount);
		builder.append(", pageNo=");
		builder.append(pageNo);
		builder.append(", rowCount=");
		builder.append(rowCount);
		builder.append(", size=");
		builder.append(size);
		builder.append("]");
		return builder.toString();
	}
}